﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class mrj_MrjControler : System.Web.UI.Page
{
    MyDbHelper mydb = new MyDbHelper();
    protected void Page_Load(object sender, EventArgs e)
    {
        string aciton = Request.Params["aciton"];
        switch (aciton)
        {
            case "pos_sale_chart":
                GetChartsData();
                break;
            case "pos_shopsale_chart":
                pos_shopsale_chart();
                break;
            case "lodshoplist":
                lodshoplist();
                break;
        }
    }

    private void pos_shopsale_chart()
    {
        string shopid = Request.Params["shopid"];
        string bdate = Request.Params["bdate"];
        string edate = Request.Params["edate"];
        string val = " ";
        if (!string.IsNullOrEmpty(shopid))
        {
            val += "(";
            string[] list = shopid.Split(',');
            foreach (string s in list)
            {
                val += "''" + s + "'',";
            }
            val = val.TrimEnd(',');
            val += ")";
        }

        DataSet ds = mydb.DM_GetDateSet("KM_MrjShopSaleKpi_Rpt '" + bdate + "','" + edate + "','" + val + "'");
        /*
         * {"kcmc":{"categories":["北京路店","武汉店","长春店","佛山店"]},
            "score":[{"data":[75,84,90,96,86,100],"name":"本期销售额"},{"data":[83,87,88,91,83,100],"name":"目标销售额"}]}
         * */
        string sbr = "";
        string sbr2 = "";
        string timeStr = "";
        string moneyStr = "";
        string counstr = "";
        string kinqty = "";
        string zhlStr = "";
        string kdj = "";
        string ldv = "";
        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            timeStr += "\"" + dr["shopname"] + "\",";
            moneyStr += dr["saleamount"].ToString() + ",";
            counstr += dr["salecount"].ToString() + ",";
            kinqty += dr["kinqty"].ToString() + ",";
            zhlStr += dr["zhl"].ToString() + ",";
            kdj += dr["salekdj"].ToString() + ",";
            ldv += dr["saleldv"].ToString() + ",";
            sbr2 += "{\"shopname\":\"" + dr["shopname"]
                    + "\",\"saleamount\":\"" + dr["saleamount"]
                    + "\",\"saleqty\":\"" + dr["saleqty"]
                    + "\",\"salecount\":\"" + dr["salecount"]
                    + "\",\"kinqty\":\"" + dr["kinqty"]
                    + "\",\"zhl\":\"" + dr["zhl"]
                    + "\",\"salekdj\":\"" + dr["salekdj"]
                    + "\",\"saleldv\":\"" + dr["saleldv"]
                    + "\",\"salejdj\":\"" + dr["salejdj"]
                    + "\",\"kddate\":\"" + dr["kddate"]
                        + "\"},";
        }
        sbr = "{\"timelist\":{\"categories\":[" + timeStr.TrimEnd(',') + "],labels: {rotation: -20 }}," +
        "\"series\":[{\"data\":[" + moneyStr.TrimEnd(',') + "],\"name\":\"销售额（百元）\"},{\"data\":["
        + counstr.TrimEnd(',') + "],\"name\":\"销售单数（单）\"},{\"data\":[" + zhlStr.TrimEnd(',')
        + "],\"name\":\"转换率（%）\"},{\"data\":[" + kinqty.TrimEnd(',') + "],\"name\":\"客流量\"},{\"data\":["
        + kdj.TrimEnd(',') + "],\"name\":\"客单价（元）\"},{\"data\":["
        + ldv.TrimEnd(',') + "],\"name\":\"连带率\"}],\"datalist\":[" + sbr2 + "]}";
        Response.Write(sbr);//返回礼品列表
        Response.End();
        return;
    }
    private void lodshoplist()
    {
        string shopname = Request.Params["shopname"];
        string str = "";
        if (shopname != "")
        {
            str += " and mrjshopname like '%" + shopname + "%'";
        }
        DataSet ds = mydb.DM_GetDateSet(@"select kmshopid,mrjshopname from Bas_mrj_Shop s
        inner join V_UserShopDataRights b on b.shopid = s.kmshopid where 1=1 and userid="+Session["UserID"]+" " + str + " order by mrjshopid");
        if (ds.Tables[0].Rows.Count > 0)
        {
            string sbr = "";
            sbr = "[";
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                sbr += "{\"shopname\":\"" + dr["mrjshopname"]
                    + "\",\"shopid\":\"" + dr["kmshopid"]
                    + "\"},";
            }
            sbr = sbr.TrimEnd(',') + "]";
            Response.Write(sbr);
            Response.End();
        }
        else
        {
            Response.Write("NO_DATA");
            Response.End();
        }

    }
    private void GetChartsData()
    {
        string shopid =Request.Params["shopid"];
        string bdate = Request.Params["bdate"];
        string edate = Request.Params["edate"];
        string datetype = Request.Params["stype"];
        string val = " ";
        if (!string.IsNullOrEmpty(shopid))
        {
            val += "(";
            string[] list = shopid.Split(';');
            foreach (string s in list)
            {
                val += "''" + s + "'',";
            }
            val = val.TrimEnd(',');
            val += ")";
        }

        DataSet ds = mydb.DM_GetDateSet("KM_MrjSaleKpi_Rpt '" + bdate + "','"+edate+"','" + shopid + "','" + datetype + "'");
        /*
         * {"kcmc":{"categories":["北京路店","武汉店","长春店","佛山店"]},
            "score":[{"data":[75,84,90,96,86,100],"name":"本期销售额"},{"data":[83,87,88,91,83,100],"name":"目标销售额"}]}
         * */
        string sbr = "";
        string sbr2 = "";
        string timeStr = "";
        string moneyStr = "";
        string counstr = "";
        string kinqty = "";
        string zhlStr = "";
        string kdj = "";
        string ldv = "";
        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            timeStr += "\"" + dr["bdate"] + "\",";
            moneyStr += dr["saleamount"].ToString() + ",";
            counstr += dr["salecount"].ToString() + ",";
            kinqty += dr["kinqty"].ToString() + ",";
            zhlStr += dr["zhl"].ToString() + ",";
            kdj += dr["salekdj"].ToString() + ",";
            ldv += dr["saleldv"].ToString() + ",";
            sbr2 += "{\"bdate\":\"" + dr["bdate"]
                    + "\",\"saleamount\":\"" + dr["saleamount"]
                    + "\",\"saleqty\":\"" + dr["saleqty"]
                    + "\",\"salecount\":\"" + dr["salecount"]
                    + "\",\"kinqty\":\"" + dr["kinqty"]
                    + "\",\"zhl\":\"" + dr["zhl"]
                    + "\",\"salekdj\":\"" + dr["salekdj"]
                    + "\",\"saleldv\":\"" + dr["saleldv"]
                    + "\",\"salejdj\":\"" + dr["salejdj"]
                        + "\"},";
        }
        sbr = "{\"timelist\":{\"categories\":[" + timeStr.TrimEnd(',') + "],labels: {rotation: 0 }}," +
        "\"series\":[{\"data\":[" + moneyStr.TrimEnd(',') + "],\"name\":\"销售额（百元）\"},{\"data\":["
        + counstr.TrimEnd(',') + "],\"name\":\"销售单数（单）\"},{\"data\":[" + zhlStr.TrimEnd(',')
        + "],\"name\":\"转换率（%）\"},{\"data\":[" + kinqty.TrimEnd(',') + "],\"name\":\"客流量\"},{\"data\":[" 
        + kdj.TrimEnd(',') + "],\"name\":\"客单价（元）\"},{\"data\":[" 
        + ldv.TrimEnd(',') + "],\"name\":\"连带率\"}],\"datalist\":["+sbr2+"]}";
        Response.Write(sbr);//返回礼品列表
        Response.End();
        return;
    }
}
